<h1>Admin</h1>

<div style="border: 1px solid #999">
	<a href="index.php?action=admin&sub_action=inserisci">inserisci nuova pagina</a>
</div>




<?php
require_once 'funzioni.php';

connettidb();

switch ($_REQUEST ['sub_action'])
{
	case 'modifica' : modificaPagina(); break;
	case 'inserisci' :visualizzaForm(); break;
	case 'inseriscidb' :inserisciPaginadb(); break;
	default:adminListPages(); break;
}	

function inserisciPaginadb()
{
	$sql="INSERT INTO pages(title_it, title_en,content_it,content_en,section)
		VALUES ('$_POST[title_it]', '$_POST[title_en]',
		'$_POST[content_it]','$_POST[content_en]','$_POST[section]')";
	echo $sql;
}




function visualizzaForm($dati=array(), $sub_action='inseriscidb')
{
	include 'config.php';
	?>
	
	<form action="index.php?action=admin" method="post">
	<input type="hidden" name="sub_action" value='<?php echo $sub_action;?>'/>
	<input type="hidden" name="id" value='<?php echo $dati['id'];?>'/>
	
	<label>titolo (it)</label>
	<input type="text" name="title_it" value='<?php echo $dati['title_it'];?>'/><br />
	
	<label>titolo (en)</label>
	<input type="text" name="title_en" value='<?php echo $dati['title_en'];?>'/><br />
	
	<label>contenuto (it)</label>
	<textarea rows="5" cols="50" name="content_it"><?php echo $dati['content_it'];?></textarea><br />
	
	<label>contenuto (en)</label>
	<textarea rows="5" cols="50" name="content_en"><?php echo $dati['content_en'];?></textarea><br />
	
	<label>sezione</label>
	<select name="section">
	<?php 
		foreach($sections as $sezione=>$sottosezioni)
		{
			printf('<option value="%s">%s</option>',$sezione, $sezione);  //%s sta per segnaposto
		}	
	
	
	?>
	</select>
	<input type="submit" value="invia" />
	
	
	</form>
	
	
	
	<?php 
}

function adminListPages()
{
	$sql= "SELECT * FROM pages ORDER BY title_it ASC";
	$result = mysql_query($sql);
	if(mysql_num_rows($result))
	{
		echo '<table>
				<tr>
					<th>Id</th>
					<th>titolo</th>
					<th>Contenuto</th>
				</tr>';
		
		while ($row=mysql_fetch_assoc($result))
		{
			printf('<tr>
				   <th>%s</th>
				   <th>%s</th>
				   <th>%s</th>
				   </tr>',$row['id'], $row['title_it'],substr($row['content_it'],0,50));
		}
		echo'</table>';	
	
	}
	
}
	
	
?>

